﻿package com.firewolf.zip.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;
public class ZipUI extends JFrame{
	private final int DEFAULT_WIDTH = 400;  //界面宽度
	private final int DEFAULT_HEIGHT = 300; //界面高度
	private final int LOCATION_X = 200;		//左边界距离
	private final int LOCATION_Y = 200;		//右边界距离
	private JComboBox fileComboBox;
	private JTextArea jTextArea;
	private String fileName;
	private JMenuBar jMenuBar; // 菜单栏
	private JMenu fileMenu; //菜单
	private JMenuItem openFileItem;//菜单项
	private JMenuItem exitItem;//菜单项
	public ZipUI() {
		setTitle("Zip测试"); //标题
		
		//位置和大小
		setBounds(LOCATION_X,LOCATION_Y,DEFAULT_WIDTH,DEFAULT_HEIGHT);
		
		//菜单
		jMenuBar = new JMenuBar();
		fileMenu = new JMenu("文件");
		openFileItem = new JMenuItem("打开");
		openFileItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser chooser = new JFileChooser();
//				chooser.setMultiSelectionEnabled(true);
				chooser.setCurrentDirectory(new File("."));
				int r = chooser.showOpenDialog(ZipUI.this);
				if(r == JFileChooser.APPROVE_OPTION){
					fileName = chooser.getSelectedFile().getPath();
					fileComboBox.removeAllItems();
					scanZipFile();
				}
			}	
		});
		exitItem = new JMenuItem("退出");
		exitItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		fileMenu.add(openFileItem);
		fileMenu.add(exitItem);
		jMenuBar.add(fileMenu);
		setJMenuBar(jMenuBar);
		
		jTextArea = new JTextArea();
		fileComboBox = new JComboBox();
		fileComboBox.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				loadZipFile((String)fileComboBox.getSelectedItem());
			}
		});
		
		add(fileComboBox,BorderLayout.SOUTH);
		add(new JScrollPane(jTextArea),BorderLayout.CENTER);
	}
	
	//读取zip文件
	private void scanZipFile() {
		new SwingWorker<Void, String>(){

			@Override
			protected Void doInBackground() throws Exception {
				ZipInputStream zin = new ZipInputStream(new FileInputStream(fileName));
				ZipEntry entry;
				while((entry = zin.getNextEntry()) != null){
					publish(entry.getName());
					zin.closeEntry();
				}
				zin.close();
				return null;
			}

			@SuppressWarnings("unchecked")
			@Override
			protected void process(List<String> names) {
				for(String name : names){
					fileComboBox.addItem(name);
				}
			}
			
		}.execute();
	}
	
	
	private void loadZipFile(final String name){
		fileComboBox.setEnabled(false);
		jTextArea.setText("");
		new SwingWorker<Void, Void>(){

			@Override
			protected Void doInBackground() throws Exception {
				try {
					ZipInputStream zin = new ZipInputStream(new FileInputStream(fileName));
					ZipEntry entry;
					while((entry = zin.getNextEntry()) != null) {
						if(entry.getName().equals(name)){
							@SuppressWarnings("resource")
							Scanner in = new Scanner(zin);
							while(in.hasNextLine()){
								jTextArea.append(in.nextLine());
								jTextArea.append("\n");
							}
						}
						zin.closeEntry();
					}
					zin.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
				return null;
			}

			@Override
			protected void done() {
				fileComboBox.setEnabled(true);
			}
		}.execute();
	}
}
